package com.css.dzj.zfjg.modules.fb.service.impl;

import com.css.dzj.zfjg.modules.fb.entity.JgFbLhjgEntity;
import com.css.dzj.zfjg.modules.fb.entity.QJgFbLhjgEntity;
import com.css.dzj.zfjg.modules.fb.mapper.JgFbLhjgMapper;
import com.css.dzj.zfjg.modules.fb.service.JgFbLhjgService;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class JgFbLhjgServiceImpl implements JgFbLhjgService {
    @Autowired
    private JgFbLhjgMapper fbLhjgMapper;
    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @Override
    public QueryResults<JgFbLhjgEntity> queryPage(Pageable pageable) {
        QJgFbLhjgEntity q = QJgFbLhjgEntity.jgFbLhjgEntity;
        Predicate predicate = q.isNotNull().or(q.isNull());
        /**
         * 查询条件
         */
        //predicate = (StringUtils.isEmpty(objectname)) ? predicate : ExpressionUtils.and(predicate, q.objectname.like("%" + objectname + "%"));
        // 返回结果
        return jpaQueryFactory.selectFrom(q).where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
    }

    @Override
    public JgFbLhjgEntity getById(String fbId) {
        return fbLhjgMapper.getOne(fbId);
    }

    @Override
    public void save(JgFbLhjgEntity fbLhjgEntity) {
        fbLhjgMapper.save(fbLhjgEntity);
    }

    /**
     * 修改
     * @author wt
     * @date 2020\2\21 0021
     */
    @Override
    @Transactional
    public void update(JgFbLhjgEntity fbLhjgEntity) {

    }

    @Override
    public void deleteById(String id) {
        fbLhjgMapper.deleteById(id);
    }

    @Override
    public JgFbLhjgEntity getByTaskno(String taskno) {
        QJgFbLhjgEntity q = QJgFbLhjgEntity.jgFbLhjgEntity;
        return jpaQueryFactory.selectFrom(q).where(q.taskCode.eq(taskno)).fetchOne();
    }

    @Override
    public int dbcxbCount(String fbBureau, String fbState) {
        QJgFbLhjgEntity q = QJgFbLhjgEntity.jgFbLhjgEntity;
        long count = jpaQueryFactory.selectFrom(q).where(q.fbAreaBureau.eq(fbBureau), q.fbState.eq(fbState)).fetchCount();
        return (int) count;
    }
}
